package com.hd.trans.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


import com.hd.trans.R;
import com.hd.trans.utils.DensityUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 字母表View
 *
 * @author yangHuang
 */
public class LetterSlideView extends View {

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    private Context mContext;
    private int topPadding;
    private int txtSize;

    private List<String> letterList = new ArrayList<>();

    public LetterSlideView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        initData();
    }

    public LetterSlideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initData();
    }

    public LetterSlideView(Context context) {
        super(context);
        mContext = context;
        initData();
    }

    private void initData() {
        letterList = Arrays.asList(LETTERS_DEFAULT);

        paintBg.setColor(Color.parseColor("#FFFFFF"));
        topPadding = DensityUtils.dip2px(mContext, 6);
        txtSize = DensityUtils.dip2px(mContext, 10);
    }


    // 26个字母
    public static String[] LETTERS_DEFAULT = {"A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z"};

    private OnTouchingChangedListener onTouchingLetterChangedListener;
    private OnPinyinSelectedListener onPinyinSelectListener;

    int choose = -1;

    Paint paint = new Paint();
    Paint paintBg = new Paint();
    Rect rect = new Rect();

    public void setLetterList(ArrayList<String> letters) {
        if(letters==null){
            this.letterList = new ArrayList<>() ;
        }else {
            this.letterList = letters;
        }
        invalidate();
    }


    /**
     * 重写这个方法
     */
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(letterList == null || letterList.size() == 0){
            return;
        }
        if (showBkg) {
//			canvas.drawColor(Color.parseColor("#40000000"));
        }

        int height = getHeight();
        int width = getWidth();
        //上下个空出半个字母高度
        float singleHeight = (height - letterList.size()) / letterList.size();
        for (int i = 0; i < letterList.size(); i++) {
            paint.setColor(Color.parseColor("#FF9DA9B9"));
            // paint.setColor(Color.WHITE);
            //paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            paint.setTextSize(txtSize);
            paint.getTextBounds(letterList.get(i), 0, 1, rect);
            if (i == choose) {
                paint.setColor(Color.parseColor("#FF9DA9B9"));
                paint.setFakeBoldText(true);
//				canvas.drawRect(0, singleHeight*(i+1)-rect.height()-topPadding, width, singleHeight*(i+1)+topPadding, paintBg);
            }
            float xPos = width / 2 - paint.measureText(letterList.get(i)) / 2;
            float yPos = singleHeight * (i + 1);
            canvas.drawText(letterList.get(i), xPos, yPos, paint);
            paint.reset();
        }

    }

    private boolean showBkg = false;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = choose;
        final OnTouchingChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) (y / getHeight() * letterList.size());

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                setBackgroundResource(R.drawable.sidebar_background);
                showBkg = true;
                choose = -1;
                if (listener != null) {
                    if (c >= 0 && c < letterList.size()) {
                        listener.onTouchingChanged(letterList.get(c));
                        choose = c;
                    }
                }
                invalidate();
                break;

            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < letterList.size()) {
                        listener.onTouchingChanged(letterList.get(c));
                        choose = c;
                        invalidate();
                    }
                }
                break;

            case MotionEvent.ACTION_UP:
                setBackgroundColor(Color.parseColor("#FFFFFF"));
                showBkg = false;
                //choose = -1;
                if (onPinyinSelectListener != null) {
                    if (c >= 0 && c < letterList.size()) {
                        onPinyinSelectListener.OnPinyinSelected(letterList.get(c));
                        choose = c;
                    }
                }
                invalidate();
                break;
        }
        return true;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    /**
     * 向外公开的方法
     *
     * @param onTouchingLetterChangedListener
     */
    public void setOnTouchingChangedListener(
            OnTouchingChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    //拼音滑动改变
    public interface OnTouchingChangedListener {
        public void onTouchingChanged(String s);
    }

    //拼音选中
    public interface OnPinyinSelectedListener {
        public void OnPinyinSelected(String s);
    }

    //onPinyinSelectListener
    public void setOnPinyinSelectedListener(
            OnPinyinSelectedListener listener) {
        this.onPinyinSelectListener = listener;
    }

    public void clearPinyin() {
        choose = -1;
        invalidate();
    }

}
